﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace TomNet.Core.Options
{
    /// <summary>
    /// TomNet 框架配置选项信息
    /// </summary>
    public class TomNetOptions
    {
        /// <summary>
        /// 初始化一个<see cref="TomNetOptions"/>类型的新实例
        /// </summary>
        public TomNetOptions()
        {
            DbContexts = new ConcurrentDictionary<string, TomNetDBContextOptions>(StringComparer.OrdinalIgnoreCase);
            UploadPolicies = new ConcurrentDictionary<string, UploadOptions>(StringComparer.OrdinalIgnoreCase);
        }

        /// <summary>
        /// SendGrid配置
        /// </summary>
        public SendGridOptions SendGrid { get; set; }

       
        /// <summary>
        /// 获取 数据上下文配置信息
        /// </summary>
        public IDictionary<string, TomNetDBContextOptions> DbContexts { get; }

        /// <summary>
        /// 获取 上传策略配置信息
        /// </summary>
        public IDictionary<string, UploadOptions> UploadPolicies { get; }
        /// <summary>
        /// 获取或设置 JWT身份认证选项
        /// </summary>
        public JwtOptions Jwt { get; set; }

        /// <summary>
        /// 获取或设置 Redis选项
        /// </summary>
        public RedisOptions Redis { get; set; }

        /// <summary>
        /// 获取或设置 Swagger选项
        /// </summary>
        public SwaggerOptions Swagger { get; set; }

        /// <summary>
        /// 获取或设置 跨域 选项
        /// </summary>
        public CrossOptions Cross { get; set; }

        /// <summary>
        /// 获取指定上下文类和指定数据库类型的上下文配置信息
        /// </summary>
        public TomNetDBContextOptions GetDbContextOptions(string dbContextKey)
        {
            return DbContexts.Values.SingleOrDefault(m => m.DatabaseKey == dbContextKey);
        }

        /// <summary>
        /// 获取指定策略名称的上传策略配置信息
        /// </summary>
        public UploadOptions GetUploadOptions(string policyName)
        {
            return UploadPolicies.Values.SingleOrDefault(m => m.PolicyName == policyName);
        }
    }
}